package com.sinosoft.dsp.component.bpm.viewbean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jbpm.api.Deployment;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;

import com.sinosoft.dsp.component.bpm.facade.engine.ProcessResourceFacade;
import com.sinosoft.dsp.component.bpm.util.DeploymentFileType;
import com.sinosoft.dsp.component.bpm.util.JbpmConfig;
import com.sinosoft.dsp.platform.components.portal.iteration.SinoAbstractTreeViewBean;
import com.sinosoft.dsp.platform.components.portal.iteration.SinoTreeNodeType;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.core.exception.SinoException;
import com.sinosoft.dsp.system.core.property.BasePropertyHandler;
import com.sinosoft.dsp.system.utils.Const;

public class BpmConfigViewBean extends
		SinoAbstractTreeViewBean<BpmConfigDataObject> implements
		IBpmConfigViewBean {

	public static final String deployType = BasePropertyHandler
			.getProperty("bpm.config.deployType");
	public static final String deployId = BasePropertyHandler
			.getProperty("bpm.config.deploy.singleton.id");

	@Override
	public void construct(String sceneId, String content) throws SinoException {
		super.construct(sceneId, content);

		for (DeploymentFileType type : DeploymentFileType.values()) {
			SinoTreeNodeType nodeType = new SinoTreeNodeType(type.getName());
			nodeType.setExpandedIcon(type.getExpandedIcon());
			nodeType.setCollapsedIcon(type.getCollapsedIcon());
			nodeType.setIcon(type.getLeafIcon());
			this.addTreeNodeType(nodeType);
		}
	}

	@Override
	public void onNodeSelect(NodeSelectEvent event) {
		if (!this.getSelectedNode().getType().startsWith(
				DeploymentFileType.PREFIX_FILE)) {
			setCurrentNode(this.getSelectedNode());
		} else {
			setCurrentNode((DefaultTreeNode) this.getSelectedNode().getParent());
		}
	}

	@Override
	public List<BpmConfigDataObject> load() {
		ProcessResourceFacade pr = SinoManagedBean
				.findBean("processResourceFacade");
		JbpmConfig jc = SinoManagedBean.findBean("jbpmConfig");

		List<BpmConfigDataObject> bpmList = new ArrayList<BpmConfigDataObject>();

		// 判断BPM部署的方式
		if ("singleton".equalsIgnoreCase(deployType)) {
			Deployment d = pr.queryActiveDeploymentById(deployId);
			if (d != null) {
				BpmConfigDataObject bpm = new BpmConfigDataObject();
				bpm.setId(d.getId());
				bpm.setName(d.getName());
				bpm.setAlt("");
				bpm.setParentId(null);
				bpm.setFileType(DeploymentFileType.DEPLOYMENT);
				bpm.setData(d.getId(), null);
				bpm.resetLargeIcon();
				bpmList.add(bpm);

				// 资源
				loadResource(bpmList, d.getId(), jc.getRepositoryService()
						.getResourceNames(d.getId()));
			}

		} else {
			// 获得并添加所有部署
			List<Deployment> deploys = pr.queryActiveDeployment();
			if (deploys != null)
				for (Deployment d : deploys) {
					BpmConfigDataObject bpm = new BpmConfigDataObject();
					bpm.setId(d.getId());
					bpm.setName(d.getName());
					bpm.setAlt("");
					bpm.setParentId(d.getParentDeploymentId());
					bpm.setFileType(DeploymentFileType.DEPLOYMENT);
					bpm.setData(d.getId(), null);
					bpm.resetLargeIcon();
					bpmList.add(bpm);

					// 资源
					loadResource(bpmList, d.getId(), jc.getRepositoryService()
							.getResourceNames(d.getId()));
				}
		}
		return bpmList;
	}

	private void loadResource(List<BpmConfigDataObject> bpmList, String pid,
			Set<String> names) {
		int index = 0;
		List<BpmConfigDataObject> addList = new ArrayList<BpmConfigDataObject>();
		Map<String, String> map = new HashMap<String, String>();
		if (names != null)
			for (String name : names) {
				if (!Const.isEmpty(name)) {
					name = name.replaceAll("\\\\", "/");
					String[] paths = name.split("/");

					String thisId = pid + "-" + index++;

					// 目录
					if (name.endsWith("/")) {
						BpmConfigDataObject bpm = new BpmConfigDataObject();
						bpm.setId(thisId);
						bpm.setName(paths[paths.length - 1]);
						bpm.setAlt(name);
						bpm.setFileType(DeploymentFileType.RESOURCE_DIRECTORY);
						bpm.setData(pid, name);
						bpm.resetLargeIcon();
						addList.add(bpm);

						map.put(name, thisId);
					} else {
						BpmConfigDataObject bpm = new BpmConfigDataObject();
						bpm.setId(thisId);
						bpm.setName(paths[paths.length - 1]);
						bpm.setAlt(name);
						bpm.setFileType(DeploymentFileType.getFileType(name));
						bpm.setData(pid, name);
						bpm.resetLargeIcon();
						addList.add(bpm);

						map.put(name, thisId);
					}

				}
			}

		for (BpmConfigDataObject bcdo : addList) {
			String parentKey = bcdo.getResourceName().substring(0,
					bcdo.getResourceName().lastIndexOf(bcdo.getName()));
			if (!Const.isEmpty(parentKey)) {
				bcdo.setParentId(map.get(parentKey));
			} else {
				bcdo.setParentId(pid);
			}
		}

		bpmList.addAll(addList);
	}
}
